/**
 * 
 */
package denaris.midlet;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

import denaris.core.Statusbar;
import denaris.factory.ImageFactory;
import denaris.midlet.DenarisGameCanvas.DisplayProperties;

/**
 * @author stkl23
 * 
 */
public class GetReadyScreen extends AbstractScreen {
	private static GetReadyScreen instance;

	private Sprite getReady = new Sprite(ImageFactory
			.getImage("/denaris/resource/sprite/get_ready.png"), 133, 14);

	private int stillDelay = 10;

	public static GetReadyScreen getInstance() {
		if (instance == null) {
			instance = new GetReadyScreen();
		}

		return instance;
	}

	protected GetReadyScreen() {
	}

	public void doFullRepaintToBuffer(Graphics g) {
		GameScreen.getInstance().doFullRepaintToBuffer(g);
		paintScreenToBuffer(g);
	}

	protected void initialize() {
		// center the sprite just above the top of the screen
		getReady.setPosition((DisplayProperties.DISPLAY_WIDTH >> 1)
				- (getReady.getWidth() >> 1), -getReady.getHeight());
		// reset the map
		GameScreen.getInstance().getCurrentMap().reset();
		// set the delay when the sprite stands still
		stillDelay = 15;
	}

	public void paintScreenToBuffer(Graphics g) {
		// paint the map
		GameScreen.getInstance().paintScreenToBuffer(g);
		// paint the sprite
		getReady.paint(g);
	}

	public void update() {
		int middle = DisplayProperties.GAME_TOP + (DisplayProperties.GAME_HEIGHT >> 1);
		int deltaY = (middle - getReady.getY()) >> 2;
		if (deltaY == 0) {
			if (stillDelay == 0) {
				deltaY++;
			} else {
				stillDelay--;
			}
		}

		getReady.move(0, Math.abs(deltaY));
		
		if (getReady.getY() > DisplayProperties.GAME_TOP + DisplayProperties.GAME_HEIGHT + Statusbar.getInstance().getHeight()) {
			// we're done
			Denaris.getInstance().notifyReady();
		}
	}
}
